/**  
* @Title: BuyController.java
* @Package com.lemon.controller.crwx
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月24日
* @version V1.0  
*/
package com.lemon.controller.crwx;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.controller.BaseController;
import com.lemon.service.buy.BuyService;

/**
 * <p>Title: BuyController</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月24日
 * @版本 V 1.0 
 */
@RestController
@RequestMapping("/buy")
public class BuyController extends BaseController {

	@Resource
	private BuyService buyService;

	/**
	 * 微信支付异步回调通知
	 *
	 * @param request
	 */
	// @ApiIgnore
	@PostMapping("/payWeChatNotifyUrl")
	public String payWeChatNotifyUrl(HttpServletRequest request , HttpServletResponse response) {
		String restStr = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[error]]></return_msg></xml>";
		try {
			ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
			InputStream inStream = request.getInputStream();
			byte[] buffer = new byte[1024];
			int length;
			while ((length = inStream.read(buffer)) != -1) {
				outSteam.write(buffer , 0 , length);
			}
			outSteam.close();
			inStream.close();
			String str = new String(outSteam.toByteArray(), StandardCharsets.UTF_8);
			//String str1 = outSteam.toString(StandardCharsets.UTF_8.name());
			log.info("支付通知参数={}" , str);
			boolean success = buyService.payWeChatNotifyUrl(str);  //支付成功后
			if (success) {
				restStr = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
			}
			PrintStream out = new PrintStream(response.getOutputStream());
			out.print(restStr);
			out.flush();
		} catch (Exception e) {
			log.error("支付通知失败" , e);
		}

		return restStr;
	}

	@RequestMapping("payWeApp")
	public String payWeChatNotifyUrlApp(HttpServletRequest request , HttpServletResponse response) {
		log.info("App支付成功后回调开始.....");
		String restStr = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[error]]></return_msg></xml>";
		try {
			ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
			InputStream inStream = request.getInputStream();
			byte[] buffer = new byte[1024];
			int length;
			while ((length = inStream.read(buffer)) != -1) {
				outSteam.write(buffer , 0 , length);
			}
			outSteam.close();
			inStream.close();
			String str = new String(outSteam.toByteArray(), StandardCharsets.UTF_8);
			//String str1 = outSteam.toString(StandardCharsets.UTF_8.name());
			log.info("支付通知参数={}" , str);
			boolean success = buyService.payWeChatNotifyUrlAppRenewal(str);  //支付成功后
			if (success) {
				restStr = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
			}
			PrintStream out = new PrintStream(response.getOutputStream());
			out.print(restStr);
			out.flush();
		} catch (Exception e) {
			log.error("App支付通知失败" , e);
		}
		return restStr;
	}

	@PostMapping("/payWeRuzhu")
	public String payWeChatNotifyUrlRuzhu(HttpServletRequest request , HttpServletResponse response) {
		log.info("小程序商家入驻支付成功后回调开始.....");
		String restStr = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[error]]></return_msg></xml>";
		try {
			ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
			InputStream inStream = request.getInputStream();
			byte[] buffer = new byte[1024];
			int length;
			while ((length = inStream.read(buffer)) != -1) {
				outSteam.write(buffer , 0 , length);
			}
			outSteam.close();
			inStream.close();
			String str = new String(outSteam.toByteArray(), StandardCharsets.UTF_8);
			//String str1 = outSteam.toString(StandardCharsets.UTF_8.name());
			log.info("支付通知参数={}" , str);
			boolean success = buyService.payWeChatNotifyUrlRuzhu(str);  //支付成功后
			if (success) {
				restStr = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
			}
			PrintStream out = new PrintStream(response.getOutputStream());
			out.print(restStr);
			out.flush();
		} catch (Exception e) {
			log.error("App支付通知失败" , e);
		}
		return restStr;
	}

	@PostMapping("refundNotify")
	public String refundNotifyUrl(HttpServletRequest request , HttpServletResponse response) throws IOException {
		log.info("退款回掉");
		String restStr = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[error]]></return_msg></xml>";
		try {
			ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
			InputStream inStream = request.getInputStream();
			byte[] buffer = new byte[1024];
			int length;
			while ((length = inStream.read(buffer)) != -1) {
				outSteam.write(buffer , 0 , length);
			}
			outSteam.close();
			inStream.close();
			String str = new String(outSteam.toByteArray(), StandardCharsets.UTF_8);
			//String str1 = outSteam.toString(StandardCharsets.UTF_8.name());
			log.info("退款通知参数={}" , str);
			boolean success = buyService.handelRefundNotify(str);
			if (success) {
				restStr = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
				return restStr;
			}
		} catch (Exception e) {
			log.error("退款通知失败" , e);
		}
		return restStr;
	}
}
